<template>
  <div class="login_container">
    <el-row>
      <!-- 网格系统总共有24格 
       :xs="0"表示当网页小于768时此标签不占位-->
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>hello</h1>
          <h2>欢迎来到Runner World</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              show-password
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              typ="primary"
              size="default"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { ElNotification } from 'element-plus'
import { useRoute, useRouter } from 'vue-router' //直接引入用于编程式导航
//引入用户相关的小仓库
import useUserStore from '@/store/modules/user'
import { getTime } from '@/utils/times'
let loginForms = ref()
let useStore = useUserStore()
let $router = useRouter()
let $route = useRoute()
let loading = ref(false)
let loginForm = reactive({
  username: 'admin',
  password: '111111',
})
const login = async () => {
  //验证全部表单校验通过再发请求
  //validate()对整个表单内容进行验证，接收一个回调函数，或放回一个Promise
  await loginForms.value.validate()

  //加载效果
  loading.value = true
  //通知仓库发登录请求
  try {
    await useStore.userLogin(loginForm)
    //编程式导航跳转到展示数据首页
    //判断登录的时候，路由当中是否有query参数，如果有就往query参数跳转，没有则正常跳转至首页
    let redirect: any = $route.query.redirect
    $router.push({ path: redirect || '/home' })
    //显示登录成功的提示信息
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `Hi,${getTime()}`, //注意此处为单反引号
    })
    loading.value = false
  } catch (error) {
    loading.value = false
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  }
}
//自定义校验规则函数
const validatorUserName = (_rule: any, value: any, callback: any) => {
  //rule:校验规则对象
  //value:表单元素文本内容
  //callback():如果符合条件callback执行通过，如果不符合，注入错误提示信息
  if (value.length >= 5 && value.length <= 10) {
    callback()
  } else {
    callback(new Error('账号长度为五到十位'))
  }
}

const validatorPassWord = (_rule: any, value: any, callback: any) => {
  if (value.length >= 6 && value.length <= 12) {
    callback()
  } else {
    callback(new Error('账号长度为五到十位'))
  }
}
//配置表单校验需要的校验对象
const rules = {
  //required表示此字段必须要校验
  //min,max表示文本的长度限制
  //message:错误的提示信息
  //trigger:触发校验表单的时机，change->文本发生变化时触发，blur->失去焦点时触发
  username: [
    // {required:true, min:5,max:10,message:'账号长度为五到十位',trigger:'change'}
    { trigger: 'change', validator: validatorUserName },
  ],
  password: [
    // {required:true, min:6,max:12,message:'密码长度为六到十二位',trigger:'change'}
    { trigger: 'change', validator: validatorPassWord },
  ],
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100%;
  background: url('@/assets/icons/images/微信图片_20240705111013.jpg') no-repeat
    center center;
  background-size: cover;
  display: flex;
  align-items: center;

  .el-row {
    width: 100%;
  }

  .login_form {
    width: 80%;
    margin: 30vh auto;
    padding: 40px;
    border-radius: 8px;
    background: url('@/assets/icons/images/login_form.png') no-repeat center
      center;
    background-size: cover;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);

    h1 {
      color: rgb(191, 237, 221);
      font-size: 40px;
      margin-bottom: 10px;
    }

    h2 {
      color: rgb(191, 237, 221);
      font-size: 20px;
      margin: 20px 0;
    }

    .login_btn {
      width: 100%;
      background-color: rgb(222, 183, 255);

      &:hover {
        background-color: darken(rgb(222, 183, 255), 10%);
      }
    }

    :deep(.el-input) {
      .el-input__wrapper {
        background: rgba(255, 255, 255, 0.8);
      }
    }
  }
}
</style>
